<?php
namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Http\Requests;
use App\Models\Link;
use App\Models\Placard;
use App\Models\Answer;
use App\Models\Tag;
use App\Models\UserDetail;
use App\Models\Category;
use App\Models\Question;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Home\CommonController;
use App\Http\Controllers\Home\IndexController;
use DB;

use App\Models\Carousel;

class IndexController extends CommonController
{
    /**
     * 加载前台主页面
     *
     * @return \Illuminate\Http\Response
     */

    public function index()
    {
        // 查找配置信息
        $config = DB::table('zy_config')->first();
        // 查看网站状态
        $web_status= $config['web_status'];
        // 判断网站是否关闭
        if($web_status==1)
        {
            return view('errors.503');
        }

        // 获取公告数据
        $placard_data = Placard::orderBy('p_time','desc')->take(4)->get();

        // 获取轮播图及数据
        $carousel=Carousel::orderBy('c_time','desc')->take(4)->get();

        // 获取最新发布的问题 8条 并获取其分类
        $categorys = DB::table('zy_category') ->join('zy_question','zy_category.id','=','zy_question.cate_id')->where('zy_question.status','0')->orderBy('q_time','desc')->take(8)-> get();

        // 获取发布问题用户的头像
        $userdetails = DB::table('zy_userdetails') ->join('zy_question','zy_userdetails.uid','=','zy_question.uid')-> get();

        // 获取答案最多的热搜问题 6条 并获取问题的分类名称
        $category = DB::table('zy_category') ->join('zy_question','zy_category.id','=','zy_question.cate_id')->orderBy('count','desc')->take(8)-> get();

        // 获取发布问题最多的用户 1个
       $q_user = DB::table('zy_userdetails')->orderBy('q_count','desc')->take(1)->get();
       // 获取用户发布的问题,并查询其分类
       $que = DB::table('zy_question')->where('zy_question.uid','=',$q_user[0]['uid'])->get();
       $catename = '';
       foreach($que as $k=>$v)
       {
            $cate = DB::table('zy_category')->where('zy_category.id','=',$v['cate_id'])->get();
            foreach($cate as $kk=>$vv)
            {
                $cate1 = $vv['cate_name'];
                $catename = $cate1;
            }
       }

       // 获取回答问题最多的用户 1个
       $a_user = DB::table('zy_userdetails')->orderBy('a_count','desc')->take(1)->get();

       // 获取积分最高的前五名用户
       $score =  DB::table('zy_score')->join('zy_userdetails','zy_score.uid','=','zy_userdetails.uid')->orderBy('score','desc')->take(5)->get();

        // return view('home.index.index',compact('carousel','placard_data','categorys','userdetails','category','q_user','a_user','score','catename'));

         //定义redis
        $listkey='LIST:CAROUSEL';
        $hashkey='HASH:CAROUSEL:';
        //定义一个数组，存放所有的文章数据
       $carousel  = [];
         // 如果redis中不存在，从数据库中取
        if(empty(Redis::exists($listkey))){
           // 获取轮播图及数据
        $carousel=Carousel::orderBy('c_time','desc')->take(4)->get();

          // 存放的redis中
            foreach ($carousel as $k=>$v){
               // 将文章id放入列表中
                Redis::rpush($listkey,$v->id);
               // 将文章记录存放到哈希中
                Redis::hmset($hashkey.$v->id,$v->toArray());
            }

                // 加载主页面
                return view('home.index.index',compact('carousel','placard_data','categorys','userdetails','category','q_user','a_user','score','catename'));
        }else{
            $list =  Redis::lrange($listkey,0,-1);
           // 如果存在直接取
            foreach ($list as $k=>$v){
                $carousel[] = Redis::hgetall($hashkey.$v);
            }
            // 加载主页面
            return view('home.index.index',compact('carousel','placard_data','categorys','userdetails','category','q_user','a_user','score','catename'));
        }

    }


    /**
     *  处理退出登录
     *
     * @return
     */
    public function logout()
    {
        // session(['user'=>null]);

        session()->flush();
        return redirect('/');
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function showPlacard($id)
    {

        // 获取公告中的某条数据
       $res = Placard::find($id);


        // 将公告内容返回到页面
       $p_content = "<table width='100%' height='100%'>
                        <tr>
                            <th class='tc' height='80%'>{$res['p_content']}</th>
                        </tr>
                     </table>";

        return $p_content;

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


    /**
     * 获取排行中用户的个人信息
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function showDetail($id)
    {
        // 获取传递ID用户的详情信息
        $detail = UserDetail::find($id);
        $u_score = DB::table('zy_score')->where('uid','=',$id)->first();

        // 加载用户详情页
        return view('home.user.detail',compact('detail','u_score'));
    }


}
